package com.controller.device;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.Constants;
import com.common.utils.DateUtils;
import com.common.utils.EntityConversionUtil;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;
import com.core.springSecurity.model.LoginUserDetails;
import com.github.pagehelper.PageInfo;
import com.model.base.PageResult;
import com.model.base.Result;
import com.model.domain.device.*;
import com.model.domain.machine.GetMachineInfoDO;
import com.model.domain.user.GetUserInfoDO;
import com.model.dto.device.devicePMDInfo.*;
import com.model.pojo.device.*;
import com.model.pojo.machine.MachineInfo;
import com.model.pojo.sys.SysUser;
import com.model.pojo.user.UserInfo;
import com.model.vo.device.devicePMDInfo.getDeviceInfo.GetDeviceInfoPassageVO;
import com.model.vo.device.devicePMDInfo.getDeviceInfo.GetDeviceInfoVO;
import com.model.vo.device.devicePMDInfo.getDeviceList.GetDeviceListVO;
import com.model.vo.device.devicePMDInfo.getGpsMonitorList.GetGpsMonitorListVO;
import com.model.vo.device.devicePMDInfo.getStatisticsInfo.GetStatisticsInfoVO;
import com.model.vo.device.devicePMDInfo.getVersionList.GetVersionListVO;
import com.model.vo.device.devicePMDInfo.getWorkDailyList.GetWorkDailyListVO;
import com.model.vo.device.devicePMDInfo.getWorkList.GetWorkListDetailVO;
import com.model.vo.device.devicePMDInfo.getWorkList.GetWorkListVO;
import com.service.device.*;
import com.service.machine.MachineInfoService;
import com.service.user.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Get;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2023-06-07 1:10
 **/
@Api(tags = {"PMD终端管理"})
@Slf4j
@RestController
@RequestMapping("/v1/device/pmd")
@RequiredArgsConstructor
public class DevicePMDInfoController {
    private final DevicePmdBaseInfoService devicePmdBaseInfoService;
    private final DevicePmdGpsInfoService devicePmdGpsInfoService;
    private final DevicePmdWorkRecordInfoService devicePmdWorkRecordInfoService;
    private final DevicePmdVersionInfoService devicePmdVersionInfoService;
    private final UserInfoService userInfoService;
    private final MachineInfoService machineInfoService ;
    private final DevicePmdWorkDailyRecordInfoService devicePmdWorkDailyRecordInfoService;
    @Log(title = "PMD终端管理-终端统计信息", businessType = BusinessType.SELECT)
    @ApiOperation(value = "终端统计信息")
    @PostMapping("/getStatisticsInfo")
    public Result<GetStatisticsInfoVO> getStatisticsInfo(@RequestBody @Valid GetStatisticsInfoDTO dto) {
        LoginUserDetails loginUserDetails = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUserDetails.getUser();
        Integer total = 0;
        Integer online = 0;
        Integer offline = 0;
        GetDevicePmdBaseInfoDO getDevicePmdBaseInfoDO = new GetDevicePmdBaseInfoDO();
        if(dto.getFactoryId()!=null && dto.getFactoryId()!=0){
            getDevicePmdBaseInfoDO.setFactoryUserId(dto.getFactoryId());
        }
        if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机生产厂家){
            getDevicePmdBaseInfoDO.setFactoryUserId(sysUser.getUserId());
        }
        if((dto.getFactoryId()==null || dto.getFactoryId()==0) &&sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机查看权限){
            List<Long> factoryUserIds = new ArrayList<>();
            factoryUserIds.add(0l);
            GetUserInfoDO getUserInfoDO = new GetUserInfoDO();
            getUserInfoDO.setUserType(1);
            getUserInfoDO.setDeviceType(3);
            getUserInfoDO.setSortRow("id");
            List<UserInfo> userList = userInfoService.getAllList(getUserInfoDO);
            if(userList!=null && userList.size()>0){
                factoryUserIds = userList.stream().map(UserInfo::getId).collect(Collectors.toList());
            }
            getDevicePmdBaseInfoDO.setFactoryUserIds(factoryUserIds);
        }
        //经销商帐号过滤
        if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机经销商){
            List<String> dtus = new ArrayList<>();
            dtus.add("0");
            GetMachineInfoDO getMachineInfoDO = new GetMachineInfoDO();
            getMachineInfoDO.setDealerUserId(sysUser.getUserId());
            getMachineInfoDO.setSortRow("id");
            List<MachineInfo> machineList = machineInfoService.getAllList(getMachineInfoDO);
            if(machineList!=null && machineList.size()>0){
                dtus = machineList.stream().map(MachineInfo::getTerminalNum).collect(Collectors.toList());
            }
            getDevicePmdBaseInfoDO.setDtus(dtus);
        }
        getDevicePmdBaseInfoDO.setSortRow("createTime");
        List<DevicePmdBaseInfo> deviceList = devicePmdBaseInfoService.getAllList(getDevicePmdBaseInfoDO);
        if(deviceList!=null && deviceList.size()>0){
            total = deviceList.size();
            for(DevicePmdBaseInfo baseInfo:deviceList){
                if(baseInfo.getOnLineStatus() == null || baseInfo.getOnLineStatus() ==0){
                    offline++;
                }else online++;
            }
        }
        GetStatisticsInfoVO vo = new GetStatisticsInfoVO();
        vo.setTotal(total);
        vo.setOffline(offline);
        vo.setOnline(online);
        return Result.suc(vo);
    }

    @Log(title = "PMD终端管理-播种机终端列表", businessType = BusinessType.INSERT)
    @ApiOperation(value = "播种机终端列表")
    @PostMapping("/getDeviceList")
    public Result<PageResult<List<GetDeviceListVO>>> getDeviceList(@RequestBody @Valid GetDeviceListDTO dto){
        LoginUserDetails loginUserDetails = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUserDetails.getUser();
        List<GetDeviceListVO> voList = new ArrayList<>();
        GetDevicePmdBaseInfoDO getDevicePmdBaseInfoDO = EntityConversionUtil.copy(dto,GetDevicePmdBaseInfoDO.class);
        getDevicePmdBaseInfoDO.setSortRow("createTime");
        if(dto.getFactoryId()!=null && dto.getFactoryId()!=0){
            getDevicePmdBaseInfoDO.setFactoryUserId(dto.getFactoryId());
        }
        if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机生产厂家){
            getDevicePmdBaseInfoDO.setFactoryUserId(sysUser.getUserId());
        }
        if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机查看权限){
            List<Long> factoryUserIds = new ArrayList<>();
            factoryUserIds.add(0l);
            GetUserInfoDO getUserInfoDO = new GetUserInfoDO();
            getUserInfoDO.setUserType(1);
            getUserInfoDO.setDeviceType(3);
            getUserInfoDO.setSortRow("id");
            List<UserInfo> userList = userInfoService.getAllList(getUserInfoDO);
            if(userList!=null && userList.size()>0){
                factoryUserIds = userList.stream().map(UserInfo::getId).collect(Collectors.toList());
            }
            getDevicePmdBaseInfoDO.setFactoryUserIds(factoryUserIds);
        }
        //经销商帐号过滤
        if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机经销商){
            List<String> dtus = new ArrayList<>();
            dtus.add("0");
            GetMachineInfoDO getMachineInfoDO = new GetMachineInfoDO();
            getMachineInfoDO.setDealerUserId(sysUser.getUserId());
            getMachineInfoDO.setSortRow("id");
            List<MachineInfo> machineList = machineInfoService.getAllList(getMachineInfoDO);
            if(machineList!=null && machineList.size()>0){
                dtus = machineList.stream().map(MachineInfo::getTerminalNum).collect(Collectors.toList());
            }
            getDevicePmdBaseInfoDO.setDtus(dtus);
        }
        List<DevicePmdBaseInfo> deviceList = devicePmdBaseInfoService.getPageList(getDevicePmdBaseInfoDO);
        if(deviceList!=null && deviceList.size()>0){
            deviceList.forEach(item->{
                GetDeviceListVO vo = EntityConversionUtil.copy(item,GetDeviceListVO.class);
                vo.setDeviceId(item.getId());
                String factoryName = "";
                if(item.getFactoryUserId()!=null && item.getFactoryUserId()!=0){
                    UserInfo userInfo = userInfoService.getById(item.getFactoryUserId());
                    if(userInfo!=null){
                        factoryName = userInfo.getName();
                    }
                }
                vo.setFactoryName(factoryName);
                voList.add(vo);
            });
        }
        PageInfo<DevicePmdBaseInfo> pageInfo = new PageInfo<>(deviceList);
        PageResult<List<GetDeviceListVO>> build = PageResult.<List<GetDeviceListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }


    @Log(title = "PMD终端管理-播种机终端详细", businessType = BusinessType.INSERT)
    @ApiOperation(value = "播种机终端详细")
    @PostMapping("/getDeviceInfo")
    public Result<GetDeviceInfoVO> getDeviceInfo(@RequestBody @Valid GetDeviceInfoDTO dto){
        DevicePmdBaseInfo baseInfo = devicePmdBaseInfoService.getById(dto.getDeviceId());
        if(baseInfo == null){
            return Result.fail("9999","设备不存在");
        }
        GetDeviceInfoVO  vo = EntityConversionUtil.copy(baseInfo,GetDeviceInfoVO.class);
        if(baseInfo.getLastDataTime()!=null)vo.setDataTime(baseInfo.getLastDataTime());
        int onLineStatus = 0;
        if(baseInfo.getOnLineStatus()!=null)onLineStatus = baseInfo.getOnLineStatus();
        vo.setOnLineStatus(onLineStatus);
        List<GetDeviceInfoPassageVO> passageList = new ArrayList<>();
        if(StringUtils.isNotBlank(baseInfo.getContent()) && baseInfo.getContent().split("\\|").length>12){
            String[] str = baseInfo.getContent().split("\\|");
            vo.setArea(str[11]);
            int number = Integer.parseInt(str[12]);
            for(int i=1;i<number+1;i++){
                int index = 12+i;
                String c = str[index];
                GetDeviceInfoPassageVO passageVO = new GetDeviceInfoPassageVO();
                String status = c.substring(0,1);//c.substring(c.length()-1,c.length());
                int num = Integer.parseInt(c.substring(1,c.length()));//Integer.parseInt(c.substring(0,c.length()-1));
                passageVO.setNumber(num+"");
                passageVO.setStatus(status);
                passageVO.setPassage(i+"");
                passageList.add(passageVO);
            }
        }
        vo.setPassageList(passageList);
        return Result.suc(vo);
    }


    @Log(title = "PMD终端管理-修改设备编号", businessType = BusinessType.INSERT)
    @ApiOperation(value = "修改设备编号")
    @PostMapping("/updateDtu")
    public Result<Objects> updateDtu(@RequestBody @Valid UpdateDtuDTO dto){
        DevicePmdBaseInfo baseInfo = devicePmdBaseInfoService.getById(dto.getDeviceId());
        if(baseInfo==null){
            return Result.fail("9999","设备不存在");
        }
        if(StringUtils.isBlank(dto.getNewDtu())){
            return Result.fail("9999","设备号不能为空");
        }
        if(devicePmdBaseInfoService.getByDtu(dto.getNewDtu())!=null){
            return Result.fail("9999","该号码已经存在");
        }
        baseInfo.setNewDtu(dto.getNewDtu());
        baseInfo.setChangeSwitch(1);
        devicePmdBaseInfoService.update(baseInfo);
        return  Result.suc();
    }

    @Log(title = "PMD终端管理-定位监控", businessType = BusinessType.SELECT)
    @ApiOperation(value = "定位监控")
    @PostMapping("/getGpsMonitorList")
    public Result<PageResult<List<GetGpsMonitorListVO>>> getGpsMonitorList(@RequestBody @Valid GetGpsMonitorListDTO dto){
        List<GetGpsMonitorListVO> voList = new ArrayList<>();
        List<DevicePmdGpsInfo> gpsList = new ArrayList<>();
        GetDevicePmdGpsInfoDO getDevicePmdGpsInfoDO = new GetDevicePmdGpsInfoDO();
        LoginUserDetails loginUserDetails = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUserDetails.getUser();
        if (sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机生产厂家 || sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机经销商) {
            List<String> dtus = new ArrayList<>();
            dtus.add("0");
            GetDevicePmdBaseInfoDO getDevicePmdBaseInfoDO = new GetDevicePmdBaseInfoDO();
            if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机生产厂家){
                getDevicePmdBaseInfoDO.setFactoryUserId(sysUser.getUserId());
            }else if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机经销商){
                List<Long> factoryUserIds = new ArrayList<>();
                factoryUserIds.add(0l);
                GetUserInfoDO getUserInfoDO = new GetUserInfoDO();
                getUserInfoDO.setUserType(1);
                getUserInfoDO.setDeviceType(3);
                getUserInfoDO.setSortRow("id");
                List<UserInfo> userList = userInfoService.getAllList(getUserInfoDO);
                if (userList != null && userList.size() > 0) {
                    factoryUserIds = userList.stream().map(UserInfo::getId).collect(Collectors.toList());
                }
                getDevicePmdBaseInfoDO.setFactoryUserIds(factoryUserIds);
            }
            getDevicePmdBaseInfoDO.setSortRow("id");
            List<DevicePmdBaseInfo> baseList = devicePmdBaseInfoService.getAllList(getDevicePmdBaseInfoDO);
            if(baseList!=null && baseList.size()>0){
                dtus = baseList.stream().map(DevicePmdBaseInfo::getDtu).collect(Collectors.toList());
            }
            getDevicePmdGpsInfoDO.setDtus(dtus);
        }
        try {
            if (dto.getDataType() == 1) {//正常
                getDevicePmdGpsInfoDO.setDtu(dto.getDtu());
                String tableName = "device_pmd_gps_info_";
                getDevicePmdGpsInfoDO.setPage(dto.getPage());
                getDevicePmdGpsInfoDO.setPageSize(dto.getPageSize());
                getDevicePmdGpsInfoDO.setSTime(dto.getStartTime());
                getDevicePmdGpsInfoDO.setETime(dto.getEndTime());
                tableName += dto.getStartTime().split(" ")[0].replaceAll("-", "");
                getDevicePmdGpsInfoDO.setSortRow("time");
                getDevicePmdGpsInfoDO.setSortOrder("desc");
                getDevicePmdGpsInfoDO.setTableName(tableName);
                gpsList = devicePmdGpsInfoService.getPageList(getDevicePmdGpsInfoDO);
            } else {//断点
                getDevicePmdGpsInfoDO.setDtu(dto.getDtu());
                String tableName = "device_pmd_gps_info_";
                getDevicePmdGpsInfoDO.setPage(dto.getPage());
                getDevicePmdGpsInfoDO.setPageSize(dto.getPageSize());
                getDevicePmdGpsInfoDO.setSTime(dto.getStartTime());
                getDevicePmdGpsInfoDO.setETime(dto.getEndTime());
                tableName += dto.getStartTime().split(" ")[0].replaceAll("-", "");
                getDevicePmdGpsInfoDO.setSortRow("time");
                getDevicePmdGpsInfoDO.setSortOrder("desc");
                getDevicePmdGpsInfoDO.setTableName(tableName);
                List<DevicePmdGpsInfo> tempGpsList = devicePmdGpsInfoService.getAllList(getDevicePmdGpsInfoDO);
                if (tempGpsList != null && tempGpsList.size() > 0) {
                    DevicePmdGpsInfo info2 = tempGpsList.get(0);
                    for (int i = 1; i < tempGpsList.size(); i++) {
                        DevicePmdGpsInfo info1 = tempGpsList.get(i);
                        Date date1 = info1.getTime();
                        Date date2 = info2.getTime();
                        long second = DateUtils.calSecond(date1, date2);
                        if (second > dto.getBreakPoint()) {
                            gpsList.add(info2);
                            gpsList.add(info1);
                        }
                        info2 = info1;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        voList = EntityConversionUtil.copyList(gpsList,GetGpsMonitorListVO.class);
        PageInfo<DevicePmdGpsInfo> pageInfo = new PageInfo<>(gpsList);
        PageResult<List<GetGpsMonitorListVO>> build = PageResult.<List<GetGpsMonitorListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }

    @Log(title = "PMD终端管理-作业结果", businessType = BusinessType.SELECT)
    @ApiOperation(value = "作业结果")
    @PostMapping("/getWorkList")
    public Result<PageResult<List<GetWorkListVO>>> getWorkList(@RequestBody @Valid GetWorkListDTO dto){
        List<GetWorkListVO> voList = new ArrayList<>();
        GetDevicePmdWorkRecordInfoDO getDevicePmdWorkRecordInfoDO = EntityConversionUtil.copy(dto, GetDevicePmdWorkRecordInfoDO.class);
        getDevicePmdWorkRecordInfoDO.setSortRow("createdTime");
        getDevicePmdWorkRecordInfoDO.setSortOrder("desc");
        List<DevicePmdWorkRecordInfo> recordList = devicePmdWorkRecordInfoService.getPageList(getDevicePmdWorkRecordInfoDO);
        if(recordList!=null && recordList.size()>0){
            recordList.forEach(item->{
                GetWorkListVO vo = EntityConversionUtil.copy(item,GetWorkListVO.class);
                String content = item.getContent();
                String[] c = content.split("\\|");
                List<GetWorkListDetailVO> detailList = new ArrayList<>();
                if(c.length>7){
                    int index = 7;
                    for(int i=0;i<Integer.valueOf(item.getPassageNum());i++){
                        String num = c[index+0];
                        String alarm = c[index+1];
                        int ind = i+1;
                        index = index+2;
                        GetWorkListDetailVO detailVO = new GetWorkListDetailVO();
                        detailVO.setIndex(ind+"");
                        detailVO.setSeedNum(num);
                        detailVO.setAlarmNum(alarm);
                        detailList.add(detailVO);
                    }
                }
                vo.setDetailList(detailList);
                voList.add(vo);
            });
        }
        PageInfo<DevicePmdWorkRecordInfo> pageInfo = new PageInfo<>(recordList);
        PageResult<List<GetWorkListVO>> build = PageResult.<List<GetWorkListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }


    @Log(title = "PMD终端管理-版本列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "版本列表")
    @PostMapping("/getVersionList")
    public Result<PageResult<List<GetVersionListVO>>> getVersionList(@RequestBody @Valid GetVersionListDTO dto){
        List<GetVersionListVO> voList = new ArrayList<>();
        GetDevicePmdVersionInfoDO getDevicePmdVersionInfoDO = EntityConversionUtil.copy(dto, GetDevicePmdVersionInfoDO.class);
        getDevicePmdVersionInfoDO.setSortRow("createTime");
        List<DevicePmdVersionInfo> list = devicePmdVersionInfoService.getPageList(getDevicePmdVersionInfoDO);
        if(list!=null && list.size()>0){
            voList = EntityConversionUtil.copyList(list,GetVersionListVO.class);
        }
        PageInfo<DevicePmdVersionInfo> pageInfo = new PageInfo<>(list);
        PageResult<List<GetVersionListVO>> build = PageResult.<List<GetVersionListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }

    @Log(title = "PMD终端管理-新增和修改版本信息", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增和修改版本信息")
    @PostMapping("/saveVersionInfo")
    public Result<Objects> saveVersionInfo(@RequestBody @Valid SaveVersionInfoDTO dto){
        if(dto.getId()!=null && dto.getId()!=0){
            DevicePmdVersionInfo info = devicePmdVersionInfoService.getById(dto.getId());
            if(info == null){
                return Result.fail("9999","版本不存在");
            }
            info.setFilePath(dto.getFilePath());
            info.setDtus(dto.getDtus());
            info.setNewVersion(dto.getNewVersion());
            info.setOldVersion(dto.getOldVersion());
            info.setContent(dto.getContent());
            devicePmdVersionInfoService.update(info);
        }else{
            DevicePmdVersionInfo info = new DevicePmdVersionInfo();
            info.setFilePath(dto.getFilePath());
            info.setDtus(dto.getDtus());
            info.setNewVersion(dto.getNewVersion());
            info.setOldVersion(dto.getOldVersion());
            info.setContent(dto.getContent());
            devicePmdVersionInfoService.save(info);
        }
        return Result.suc();
    }

    @Log(title = "PMD终端管理-删除版本信息", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除版本信息")
    @PostMapping("/deleteVersionInfo")
    public Result<Objects> saveVersionInfo(@RequestBody @Valid DeleteVersionInfoDTO dto){
        if(dto.getId() == null || dto.getId() == 0){
            return Result.fail("9999","参数有误");
        }
        DevicePmdVersionInfo info = devicePmdVersionInfoService.getById(dto.getId());
        if(info == null){
            return Result.fail("9999","版本不存在");
        }
        devicePmdVersionInfoService.delete(dto.getId());
        return Result.suc();
    }


    @Log(title = "PMD终端管理-厂家用户新增绑定终端", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "厂家用户新增绑定终端")
    @PostMapping("/saveDeviceInfo")
    public Result<Objects> saveDeviceInfo(@RequestBody @Valid SaveDeviceInfoDTO dto){
        LoginUserDetails loginUserDetails = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUserDetails.getUser();
        DevicePmdBaseInfo info = devicePmdBaseInfoService.getByDtu(dto.getDtu());
        if(info == null){
            return Result.fail("9999","设备不存在");
        }
        if(info.getFactoryUserId() !=null){
            return Result.fail("9999","该设备已被其他厂家绑定");
        }
        info.setFactoryUserId(sysUser.getUserId());
        devicePmdBaseInfoService.update(info);
        return Result.suc();
    }

    @Log(title = "PMD终端管理-日报列表", businessType = BusinessType.INSERT)
    @ApiOperation(value = "日报列表")
    @PostMapping("/getWorkDailyList")
    public Result<PageResult<List<GetWorkDailyListVO>>> getWorkDailyList(@RequestBody @Valid GetWorkDailyListDTO dto) {
        List<GetWorkDailyListVO> voList = new ArrayList<>();
        LoginUserDetails loginUserDetails = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUserDetails.getUser();
        GetDevicePmdWorkDailyRecordInfoDO getDevicePmdWorkDailyRecordInfoDO = EntityConversionUtil.copy(dto,GetDevicePmdWorkDailyRecordInfoDO.class);
        if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机查看权限){
            List<String> dtus = new ArrayList<>();
            dtus.add("0");
            List<Long> factoryUserIds = new ArrayList<>();
            factoryUserIds.add(0l);
            GetUserInfoDO getUserInfoDO = new GetUserInfoDO();
            getUserInfoDO.setUserType(1);
            getUserInfoDO.setDeviceType(3);
            getUserInfoDO.setSortRow("id");
            List<UserInfo> userList = userInfoService.getAllList(getUserInfoDO);
            if(userList!=null && userList.size()>0){
                factoryUserIds = userList.stream().map(UserInfo::getId).collect(Collectors.toList());
            }
            GetDevicePmdBaseInfoDO getDevicePmdBaseInfoDO = new GetDevicePmdBaseInfoDO();
            getDevicePmdBaseInfoDO.setFactoryUserIds(factoryUserIds);
            getDevicePmdBaseInfoDO.setSortRow("id");
            List<DevicePmdBaseInfo> pmdList = devicePmdBaseInfoService.getAllList(getDevicePmdBaseInfoDO);
            if(pmdList!=null && pmdList.size()>0){
                dtus = pmdList.stream().map(DevicePmdBaseInfo::getDtu).collect(Collectors.toList());
            }
            getDevicePmdWorkDailyRecordInfoDO.setDtus(dtus);
        }else if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机生产厂家 || sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机经销商){
            List<String> dtus = new ArrayList<>();
            dtus.add("0");
            GetMachineInfoDO getMachineInfoDO = new GetMachineInfoDO();
            if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机生产厂家)getMachineInfoDO.setFactoryUserId(sysUser.getUserId());
            else if(sysUser.getUserType() == Constants.SysUserUserType.TYPE_播种机经销商)getMachineInfoDO.setDealerUserId(sysUser.getUserId());
            getMachineInfoDO.setSortRow("id");
            List<MachineInfo> machineList = machineInfoService.getAllList(getMachineInfoDO);
            if(machineList!=null && machineList.size()>0){
                dtus = machineList.stream().map(MachineInfo::getTerminalNum).collect(Collectors.toList());
            }
            getDevicePmdWorkDailyRecordInfoDO.setDtus(dtus);
        }
        List<DevicePmdWorkDailyRecordInfo> recordList = devicePmdWorkDailyRecordInfoService.getPageList(getDevicePmdWorkDailyRecordInfoDO);
        if(recordList!=null && recordList.size()>0){
            recordList.forEach(item ->{
                GetWorkDailyListVO vo = EntityConversionUtil.copy(item,GetWorkDailyListVO.class);
                GetMachineInfoDO getMachineInfoDO = new GetMachineInfoDO();
                getMachineInfoDO.setTerminalNum(item.getDtu());
                getMachineInfoDO.setSortRow("id");
                List<MachineInfo> machineList = machineInfoService.getAllList(getMachineInfoDO);
                String machineName = "";
                String factoryName = "";
                if(machineList!=null && machineList.size()>0){
                    MachineInfo machineInfo = machineList.get(0);
                    if(machineInfo.getDeviceNumber()!=null )machineName+=machineInfo.getDeviceNumber();
                    if(machineInfo.getDeviceModel()!=null) machineName+=machineInfo.getDeviceModel();
                    UserInfo userInfo = userInfoService.getById(machineInfo.getFactoryUserId());
                    if(userInfo!=null)factoryName = userInfo.getName();
                }
                vo.setMachineName(machineName);
                vo.setFactoryName(factoryName);
                voList.add(vo);
            });
        }
        PageInfo<DevicePmdWorkDailyRecordInfo> pageInfo = new PageInfo<>(recordList);
        PageResult<List<GetWorkDailyListVO>> build = PageResult.<List<GetWorkDailyListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }
}
